﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace M3AI
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static DateTime Now
        {
            get
            {
                return System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "China Standard Time");
            }
        }
        public static List<string> Logs = new List<string>();

        public static void Log(params string[] msg)
        {
            try
            {
                if (null != msg)
                {
                    Logs.Add(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\t" + string.Join(",", msg));
                }
                if (Logs.Count > 1100)
                {
                    Logs.RemoveRange(0, 100);
                }
            }
            catch// (Exception ex)
            {
                //
            }

        }
        protected void Application_Start()
        {
            SettingsInitializer.InitSettings();

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

    public class SettingsInitializer
    {
        public static void InitSettings()
        {
            System.Reflection.FieldInfo fi = null;
            fi = typeof(System.Configuration.ConfigurationElement).GetField("_bReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            var flag = System.Configuration.ConfigurationManager.ConnectionStrings["ConnFlag"].ConnectionString;
            foreach (System.Configuration.ConnectionStringSettings item in System.Configuration.ConfigurationManager.ConnectionStrings)
            {
                fi.SetValue(item, false);

                item.ConnectionString = string.Format(item.ConnectionString,
                    WebUI.OtherSettings.Values[flag + "/user"],
                    WebUI.OtherSettings.Values[flag + "/pwd"]);
            }
        }
    }

}